load("@rules_rust//rust:defs.bzl", "rust_doc_test", "rust_library")
load("//bazel:defs.bzl", "rust_ic_bench", "rust_ic_test", "rust_ic_test_suite")

package(default_visibility = ["//visibility:public"])

rust_library(
    name = "execution_environment",
    srcs = glob(["src/**"]),
    compile_data = glob(["tests/test-data/**"]),
    crate_name = "ic_execution_environment",
    version = "0.9.0",
    deps = [
        # Keep sorted.
        "//packages/ic-error-types",
        "//packages/ic-heap-bytes",
        "//rs/canister_sandbox:backend_lib",
        "//rs/config",
        "//rs/crypto/prng",
        "//rs/crypto/tree_hash",
        "//rs/crypto/utils/canister_threshold_sig",
        "//rs/cycles_account_manager",
        "//rs/embedders",
        "//rs/interfaces",
        "//rs/interfaces/state_manager",
        "//rs/limits",
        "//rs/monitoring/logger",
        "//rs/monitoring/metrics",
        "//rs/phantom_newtype",
        "//rs/query_stats",
        "//rs/registry/provisional_whitelist",
        "//rs/registry/subnet_type",
        "//rs/replicated_state",
        "//rs/state_layout",
        "//rs/sys",
        "//rs/types/base_types",
        "//rs/types/management_canister_types",
        "//rs/types/types",
        "//rs/types/wasm_types",
        "//rs/utils/lru_cache",
        "//rs/utils/thread",
        "@crate_index//:candid",
        "@crate_index//:ic-vetkeys",
        "@crate_index//:lazy_static",
        "@crate_index//:more-asserts",
        "@crate_index//:num-rational",
        "@crate_index//:num-traits",
        "@crate_index//:prometheus",
        "@crate_index//:rand",
        "@crate_index//:scoped_threadpool",
        "@crate_index//:serde",
        "@crate_index//:serde_cbor",
        "@crate_index//:slog",
        "@crate_index//:strum",
        "@crate_index//:tempfile",
        "@crate_index//:threadpool",
        "@crate_index//:tokio",
        "@crate_index//:tower",
        "@crate_index//:tracing",
        "@crate_index//:wasmparser",
    ],
)

rust_ic_test(
    name = "execution_environment_test",
    timeout = "long",
    crate = ":execution_environment",
    data = [
        "//rs/universal_canister/impl:universal_canister.module",
        "//rs/universal_canister/impl:universal_canister.wasm.gz",
    ],
    env = {
        "UNIVERSAL_CANISTER_WASM_PATH": "$(rootpath //rs/universal_canister/impl:universal_canister.wasm.gz)",
        "UNIVERSAL_CANISTER_SERIALIZED_MODULE_PATH": "$(rootpath //rs/universal_canister/impl:universal_canister.module)",
    },
    proc_macro_deps = [
        # Keep sorted.
        "@crate_index//:test-strategy",
    ],
    # TODO(IDX-3164): enable on PR when flakiness issue is resolved
    #                 or we have new darwin runners in use
    tags = [
        "test_all_platforms_slow",
    ],
    deps = [
        # Keep sorted.
        "//rs/crypto/sha2",
        "//rs/interfaces",
        "//rs/registry/routing_table",
        "//rs/state_machine_tests",
        "//rs/test_utilities",
        "//rs/test_utilities/consensus",
        "//rs/test_utilities/execution_environment",
        "//rs/test_utilities/metrics",
        "//rs/test_utilities/state",
        "//rs/test_utilities/types",
        "//rs/types/types",
        "//rs/types/types_test_utils",
        "//rs/universal_canister/lib",
        "@crate_index//:assert_matches",
        "@crate_index//:flate2",
        "@crate_index//:ic-btc-interface",
        "@crate_index//:maplit",
        "@crate_index//:proptest",
        "@crate_index//:serde_bytes",
        "@crate_index//:wat",
        "@crate_index//:wirm",
    ],
)

rust_ic_test_suite(
    name = "execution_environment_misc_integration",
    size = "large",
    srcs = glob(
        ["tests/*.rs"],
        # Exclude the dts test, which has a separate dts_test target below
        # because it suffered from long running times on CI
        # so we want to experiment with different configuration options.
        exclude = ["tests/dts.rs"],
    ),
    compile_data = glob(["tests/test-data/**"]),
    data = [
        "//rs/rust_canisters/backtrace_canister:backtrace-canister",
        "//rs/rust_canisters/backtrace_canister:backtrace-canister-without-names",
        "//rs/rust_canisters/call_loop_canister:call-loop-canister",
        "//rs/tests/test_canisters/image_classification:image-classification",
        "//rs/universal_canister/impl:universal_canister.module",
        "//rs/universal_canister/impl:universal_canister.wasm.gz",
        "//rs/universal_canister/impl:universal_canister_no_heartbeat.wasm.gz",
    ],
    env = dict([
        ("BACKTRACE_CANISTER_WASM_PATH", "$(rootpath //rs/rust_canisters/backtrace_canister:backtrace-canister)"),
        ("BACKTRACE_CANISTER_WITHOUT_NAMES_WASM_PATH", "$(rootpath //rs/rust_canisters/backtrace_canister:backtrace-canister-without-names)"),
        ("CALL_LOOP_CANISTER_WASM_PATH", "$(rootpath //rs/rust_canisters/call_loop_canister:call-loop-canister)"),
        ("UNIVERSAL_CANISTER_WASM_PATH", "$(rootpath //rs/universal_canister/impl:universal_canister.wasm.gz)"),
        ("UNIVERSAL_CANISTER_NO_HEARTBEAT_WASM_PATH", "$(rootpath //rs/universal_canister/impl:universal_canister_no_heartbeat.wasm.gz)"),
        ("UNIVERSAL_CANISTER_SERIALIZED_MODULE_PATH", "$(rootpath //rs/universal_canister/impl:universal_canister.module)"),
        ("IMAGE_CLASSIFICATION_CANISTER_WASM_PATH", "$(rootpath //rs/tests/test_canisters/image_classification:image-classification)"),
    ]),
    proc_macro_deps = [
        # Keep sorted.
        "@crate_index//:test-strategy",
    ],
    tags = [
        "test_all_platforms_slow",
    ],
    deps = [
        # Keep sorted.
        ":execution_environment",
        "//packages/ic-error-types",
        "//rs/config",
        "//rs/crypto/sha2",
        "//rs/crypto/test_utils/vetkd",
        "//rs/cycles_account_manager",
        "//rs/embedders",
        "//rs/interfaces",
        "//rs/interfaces/state_manager",
        "//rs/interfaces/state_manager/mocks",
        "//rs/monitoring/metrics",
        "//rs/nns/constants",
        "//rs/registry/subnet_features",
        "//rs/registry/subnet_type",
        "//rs/replicated_state",
        "//rs/rust_canisters/canister_test",
        "//rs/state_machine_tests",
        "//rs/sys",
        "//rs/test_utilities",
        "//rs/test_utilities/execution_environment",
        "//rs/test_utilities/logger",
        "//rs/test_utilities/metrics",
        "//rs/test_utilities/types",
        "//rs/types/base_types",
        "//rs/types/management_canister_types",
        "//rs/types/types",
        "//rs/types/types_test_utils",
        "//rs/universal_canister/lib",
        "//rs/utils",
        "@crate_index//:assert_matches",
        "@crate_index//:candid",
        "@crate_index//:itertools",
        "@crate_index//:libflate",
        "@crate_index//:maplit",
        "@crate_index//:more-asserts",
        "@crate_index//:num-traits",
        "@crate_index//:proptest",
        "@crate_index//:regex",
        "@crate_index//:rstest",
        "@crate_index//:serde",
        "@crate_index//:tokio",
        "@crate_index//:wasmparser",
        "@crate_index//:wat",
        "@crate_index//:wirm",
    ],
)

# TODO: move this dts test back into the :execution_environment_misc_integration target above
# when we get its P90 running time back under 5 minutes.
rust_ic_test(
    name = "dts_test",
    size = "large",
    srcs = ["tests/dts.rs"],
    data = [
        "//rs/universal_canister/impl:universal_canister.wasm.gz",
        "//rs/universal_canister/impl:universal_canister_no_heartbeat.wasm.gz",
    ],
    env = {
        "UNIVERSAL_CANISTER_WASM_PATH": "$(rootpath //rs/universal_canister/impl:universal_canister.wasm.gz)",
        "UNIVERSAL_CANISTER_NO_HEARTBEAT_WASM_PATH": "$(rootpath //rs/universal_canister/impl:universal_canister_no_heartbeat.wasm.gz)",
        "RUST_TEST_THREADS": "4",
    },
    # The failure rate of this test jumped to over 10% in the week starting on 2025-11-17 so we marked it as flaky:
    flaky = True,
    tags = [
        "cpu:4",
        "test_all_platforms_slow",
    ],
    deps = [
        # Keep sorted.
        "//packages/ic-error-types",
        "//rs/config",
        "//rs/cycles_account_manager",
        "//rs/registry/subnet_type",
        "//rs/replicated_state",
        "//rs/state_machine_tests",
        "//rs/types/base_types",
        "//rs/types/management_canister_types",
        "//rs/types/types",
        "//rs/universal_canister/lib",
        "@crate_index//:assert_matches",
        "@crate_index//:candid",
        "@crate_index//:more-asserts",
        "@crate_index//:strum",
        "@crate_index//:wat",
    ],
)

rust_doc_test(
    name = "execution_environment_doc_test",
    crate = ":execution_environment",
    tags = [
        "test_all_platforms",
    ],
)

rust_ic_bench(
    name = "execute_inspect_message_bench",
    srcs = ["benches/system_api/execute_inspect_message.rs"],
    test_name = "execute_inspect_message_bench_test",
    deps = [
        # Keep sorted.
        ":execution_environment",
        "//rs/execution_environment/benches/lib:execution_environment_bench",
        "//rs/monitoring/logger",
        "//rs/monitoring/metrics",
        "//rs/test_utilities/types",
        "//rs/types/types",
        "@crate_index//:criterion",
    ],
)

rust_ic_bench(
    name = "execute_query_bench",
    srcs = ["benches/system_api/execute_query.rs"],
    test_name = "execute_query_bench_test",
    deps = [
        # Keep sorted.
        ":execution_environment",
        "//rs/execution_environment/benches/lib:execution_environment_bench",
        "//rs/interfaces",
        "//rs/types/types",
        "@crate_index//:criterion",
    ],
)

rust_ic_bench(
    name = "execute_update_bench",
    srcs = ["benches/system_api/execute_update.rs"],
    test_name = "execute_update_bench_test",
    deps = [
        # Keep sorted.
        ":execution_environment",
        "//packages/ic-error-types",
        "//rs/execution_environment/benches/lib:execution_environment_bench",
        "//rs/limits",
        "//rs/types/types",
        "@crate_index//:candid",
        "@crate_index//:criterion",
        "@crate_index//:serde",
    ],
)

rust_ic_bench(
    name = "wasm_instructions_bench",
    srcs = glob(["benches/wasm_instructions/*.rs"]),
    deps = [
        # Keep sorted.
        ":execution_environment",
        "//packages/ic-error-types",
        "//rs/execution_environment/benches/lib:execution_environment_bench",
        "//rs/limits",
        "//rs/types/types",
        "@crate_index//:criterion",
    ],
)

rust_ic_bench(
    name = "scheduler_bench",
    srcs = ["benches/scheduler.rs"],
    deps = [
        # Keep sorted.
        ":execution_environment",
        "//rs/config",
        "//rs/replicated_state",
        "//rs/types/base_types",
        "//rs/types/types",
        "//rs/types/types_test_utils",
        "@crate_index//:criterion",
    ],
)

rust_ic_bench(
    name = "load_simulator_canisters_bench",
    srcs = ["benches/load_simulator_canisters.rs"],
    data = [
        "//rs/rust_canisters/canister_creator:canister_creator_canister",
        "//rs/rust_canisters/load_simulator:load_simulator_canister",
    ],
    env = dict([
        ("CANISTER_CREATOR_CANISTER_WASM_PATH", "$(rootpath //rs/rust_canisters/canister_creator:canister_creator_canister)"),
        ("LOAD_SIMULATOR_CANISTER_WASM_PATH", "$(rootpath //rs/rust_canisters/load_simulator:load_simulator_canister)"),
    ]),
    deps = [
        # Keep sorted.
        "//rs/rust_canisters/canister_test",
        "//rs/state_machine_tests",
        "//rs/types/types",
        "@crate_index//:criterion",
    ],
)

rust_ic_bench(
    name = "100k_canisters_bench",
    srcs = ["benches/100k_canisters.rs"],
    data = ["//rs/rust_canisters/canister_creator:canister_creator_canister"],
    env = dict([
        ("CANISTER_CREATOR_CANISTER_WASM_PATH", "$(rootpath //rs/rust_canisters/canister_creator:canister_creator_canister)"),
    ]),
    deps = [
        # Keep sorted.
        "//rs/rust_canisters/canister_test",
        "//rs/state_machine_tests",
        "//rs/types/base_types",
        "//rs/types/types",
        "@crate_index//:criterion",
        "@crate_index//:lazy_static",
    ],
)

rust_library(
    name = "utils",
    testonly = True,
    srcs = ["benches/management_canister/utils.rs"],
    deps = [
        # Keep sorted.
        "//rs/config",
        "//rs/registry/subnet_type",
        "//rs/rust_canisters/canister_test",
        "//rs/state_machine_tests",
        "//rs/types/base_types",
        "//rs/types/types",
        "@crate_index//:candid",
        "@crate_index//:serde",
    ],
)

rust_ic_bench(
    name = "management_canister_bench",
    srcs = glob(["benches/management_canister/*.rs"]),
    data = [
        "//rs/execution_environment/benches/management_canister/test_canister",
        "//rs/universal_canister/impl:universal_canister.wasm.gz",
    ],
    env = dict([
        ("TEST_CANISTER_WASM_PATH", "$(rootpath //rs/execution_environment/benches/management_canister/test_canister)"),
        ("UNIVERSAL_CANISTER_WASM_PATH", "$(rootpath //rs/universal_canister/impl:universal_canister.wasm.gz)"),
    ]),
    deps = [
        # Keep sorted.
        "//rs/config",
        "//rs/registry/subnet_type",
        "//rs/rust_canisters/canister_test",
        "//rs/state_machine_tests",
        "//rs/types/base_types",
        "//rs/types/management_canister_types",
        "//rs/types/types",
        "//rs/universal_canister/lib",
        "@crate_index//:candid",
        "@crate_index//:criterion",
        "@crate_index//:rand",
        "@crate_index//:serde",
    ],
)
